package cn.tedu.antconlonysever.contract.dao.persist.repository.impl;

import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.util.PageInfoToPageDataConverter;
import cn.tedu.antconlonysever.contract.dao.persist.mapper.LossMapper;
import cn.tedu.antconlonysever.contract.dao.persist.repository.ILossRepository;
import cn.tedu.antconlonysever.contract.pojo.entity.Loss;
import cn.tedu.antconlonysever.contract.pojo.vo.LossListVO;
import cn.tedu.antconlonysever.contract.pojo.vo.LossStandardVO;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


import java.util.Collection;
import java.util.List;

@Slf4j
@Repository
public class LossRepositoryImpl implements ILossRepository {
    @Autowired
    private LossMapper lossMapper;
    @Override
    public int insert(Loss loss) {

        return lossMapper.insert(loss);
    }

    @Override
    public int insertBatch(List<Loss> lossList) {

        return lossMapper.insertBatch(lossList);
    }

    @Override
    public int deleteById(Long id) {

        return lossMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(Collection<Long> idList) {

        return lossMapper.deleteBatchIds(idList);
    }

    @Override
    public int update(Loss loss) {
        return lossMapper.updateById(loss);
    }

    @Override
    public int count() {

        return lossMapper.selectCount(null);
    }

    @Override
    public int countCusNo(String cusNo) {
        QueryWrapper<Loss> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("cus_no ",cusNo);
        return lossMapper.selectCount(queryWrapper);
    }

    @Override
    public int countCusNoNotId(Long id, String cusNo) {
        QueryWrapper<Loss> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("cus_no",cusNo)
                    .ne("id",id);
        return lossMapper.selectCount(queryWrapper);
    }

    @Override
    public LossStandardVO getStandardById(Long id) {

        return lossMapper.getStandardById(id);
    }

    @Override
    public PageData<LossListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询流失详情列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<LossListVO> list = lossMapper.list();
        PageInfo<LossListVO> pageInfo = new PageInfo<>(list);
        PageData<LossListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
